package com.gitee.feizns.quickstart.ding.talk.utils;

import com.aliyun.dingtalkdevicemng_1_0.models.ListActivateDevicesResponseBody;
import com.aliyun.dingtalkdevicemng_1_0.models.ListInspectInfoResponseBody;
import com.aliyun.dingtalkyida_1_0.models.GetFieldDefByUuidResponseBody;
import com.aliyun.dingtalkyida_1_0.models.GetFormListInAppResponseBody;
import com.dingtalk.api.response.OapiV2UserListResponse;
import com.gitee.feizns.quickstart.jackson.JsonUtils;
import com.taobao.api.TaobaoResponse;

import java.util.Objects;

/**
 * @author feizns
 * @since 2024/03/18
 */
public class Utils {

    /**
     * 是否是角色组编号
     * @param roleGroupId 角色群编号
     * @return boolean
     */
    public static boolean isRoleGroup(Long roleGroupId) {
        return Objects.equals(roleGroupId, -1L);
    }

    /**
     * 如果错误就抛出异常
     * @param resp {@link TaobaoResponse}
     * @return {@link R}
     */
    public static <R extends TaobaoResponse> R ifErrorThrow(R resp) {
        if ( !resp.isSuccess() )
            throw new RuntimeException(resp.getBody());
        return resp;
    }

    /**
     * 如果错误就抛出异常
     * @param resp {@link TaobaoResponse}
     * @return {@link R}
     */
    public static <R extends GetFormListInAppResponseBody> R ifErrorThrow(R resp) {
        if ( !resp.getSuccess() )
            throw new RuntimeException(JsonUtils.toJson(resp.getResult()));
        return resp;
    }

    /**
     * 如果错误就抛出异常
     * @param resp {@link ListActivateDevicesResponseBody}
     * @return {@link R}
     */
    public static <R extends ListActivateDevicesResponseBody> R ifErrorThrow(R resp) {
        if ( !resp.getSuccess() )
            throw new RuntimeException(JsonUtils.toJson(resp.getResult()));
        return resp;
    }

    /**
     * 如果错误就抛出异常
     * @param resp {@link ListInspectInfoResponseBody}
     * @return {@link R}
     */
    public static <R extends ListInspectInfoResponseBody> R ifErrorThrow(R resp) {
        if ( !resp.getSuccess() )
            throw new RuntimeException(JsonUtils.toJson(resp.getResult()));
        return resp;
    }

    /**
     * 如果错误就抛出异常
     * @param resp {@link GetFieldDefByUuidResponseBody}
     * @return {@link R}
     */
    public static <R extends GetFieldDefByUuidResponseBody> R ifErrorThrow(R resp) {
        if ( !resp.getSuccess() )
            throw new RuntimeException(JsonUtils.toJson(resp.getResult()));
        return resp;
    }

    /**
     * 如果错误就抛出异常
     * @param resp {@link GetFieldDefByUuidResponseBody}
     * @return {@link R}
     */
    public static <R extends OapiV2UserListResponse> R ifErrorThrow(R resp) {
        if ( !resp.isSuccess() )
            throw new RuntimeException(JsonUtils.toJson(resp.getResult()));
        return resp;
    }

}
